package com.examp.dy.dy.controller;
import com.examp.dy.dy.dao.AccountsRepository;
import com.examp.dy.dy.dao.UserRepository;
import com.examp.dy.dy.entities.Accounts;
import com.examp.dy.dy.entities.User;
import com.examp.dy.dy.service.BookService;
import com.examp.dy.dy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("register")
public class SecurityController {
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    AccountsRepository accountsRepository;

    @Autowired
    UserRepository userRepository;
    @GetMapping("/new/account")
    public String registera(Model model){
       Accounts accounts = new Accounts();
        model.addAttribute("Accounts",accounts);
        return  "security/register";
    }
    @GetMapping("/new/user")
    public String registeru(Model model){
        User user = new User();
        model.addAttribute("User",user);
        return  "security/registerUser";
    }


    @PostMapping("/save/account")
    public String saveaccounts(Model model,Accounts Accounts){
        Accounts.setPassword(bCryptPasswordEncoder.encode(Accounts.getPassword()));
        accountsRepository.save(Accounts);
           return "redirect:/login";
    }
    @PostMapping("/save/user")
    public String saveUser(Model model,User user){
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        userRepository.save(user);
        return "redirect:/login";
    }
}
